<?php 
require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action 
{
    
	
	private $main;
	private $menu;
	
	public function preDispatch(){
		$this->main = MF_Main::getInstance();
		$this->menu = $this->main->Menu->getPointById(1);
		$this->view->menu = $this->menu;
	}
	
	public function indexAction(){
		$this->pageRender($this->menu->page);
	}
	
	public function pageAction(){
		if (NULL === $id = $this->getRequest()->getParam('id')) {
			throw new Zend_Controller_Action_Exception('Document not found', 404);
		}
		$menu = $this->main->Menu->getPointById($id);
		
		$this->view->menu_name = $menu->name;
		
		$this->pageRender($menu->page);
	}

	private function pageRender(SinglePage $page) {
		$this->view->keywords = $page->keywords;
		$this->view->description = $page->description;
		$this->view->headTitle($page->title);
		switch ($page->template_name) {
			case 'start_page':
	
				break;
		}
	}
	
	private function sendMail($post,$sendmailObj) {
		$msg_header = '<html><head><title>Заявка на проведение мероприятия</title>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		</head><body>%s</body></html>';
		$msg_message = '<table border="0" cellpadding="4" cellspacing="0">
		<tr><td valign="top">Имя:</td><td>'.$post['name'].'</td></tr>
		<tr><td valign="top">Контактный телефон:</td><td>'.$post['phone'].'</td></tr>
		<tr><td valign="top">E-Mail:</td><td>'.$post['email'].'</td></tr>
		<tr><td valign="top">Тип мероприятия:</td><td>'.$post['service'].'</td></tr>
		<tr><td valign="top">Сообщение:</td><td>'.$post['comment'].'</td></tr>
		</table>';
	
		$msg_archive = $sendmailObj->add();
		$msg_archive->from = $post['name'];
		$msg_archive->message = $msg_message;
	
		$client_mail = new Fooup_Mail('UTF-8');
		$client_mail->setBodyHtml(sprintf($msg_header, $msg_message),'UTF-8',Zend_Mime::ENCODING_BASE64);
		$client_mail->addTo($sendmailObj->email, 'Ведущий');
		$client_mail->setFrom('no-reply@я-ведущий.рф',"{$post['name']}");
		$client_mail->setSubject('Заявка на проведение мероприятия');
		$client_mail->send();
	}
	
	private function makeContentGallery(SingleGallery $gallery){
		$array = array();
		for($i=0;$i<$gallery->length();$i++){
			$item = $gallery->image($i);
			$array[] = array(
					'name'=>$item->name,
					'add_date'=>$item->add_date,
					'cover_id'=>$item->main->cover_id,
					'img_url' =>'/image/cover/id/'.$item->main->cover_id.'/dummy.jpg',
					'thumb_id'=>$item->thumb->cover_id,
					'thumb_url'=>'/image/cover/id/'.$item->thumb->cover_id.'/dummy.jpg'
			);
		}
	
		return $array;
	}


	
}

class Fooup_Mail extends Zend_Mail
{
	/**
	 * Encode a given string with the QUOTED_PRINTABLE mechanism
	 *
	 * @param string $str
	 * @param int $lineLength Defaults to {@link LINELENGTH}
	 * @param int $lineEnd Defaults to {@link LINEEND}
	 * @return string
	 */
	protected function encodeQuotedPrintable($str, $lineLength = Zend_Mime::
			LINELENGTH)
	{
		$out = '';
		$str = str_replace('=', '=3D', $str);
		$str = str_replace(Zend_Mime::$qpKeys, Zend_Mime::$qpReplaceValues, $str);
		$str = rtrim($str);
		// Split encoded text into separate lines
		while ($str) {
			$ptr = strlen($str);
			if ($ptr > $lineLength) {
				$ptr = $lineLength;
			}
			// Ensure we are not splitting across an encoded character
			$pos = strrpos(substr($str, 0, $ptr), '=');
			if ($pos !== false && $pos >= $ptr - 2) {
				$ptr = $pos;
			}
			// Check if there is a space at the end of the line and rewind
			if ($ptr > 0 && $str[$ptr - 1] == ' ') {
				--$ptr;
			}
			// Add string and continue
			$out .= substr($str, 0, $ptr);
			$str = substr($str, $ptr);
		}
		return $out;
	}

	/**
	 * Encode header fields
	 *
	 * Encodes header content according to RFC1522 if it contains non-printable
	 * characters.
	 *
	 * @param  string $value
	 * @return string
	 */
	protected function _encodeHeader($value)
	{
		if (Zend_Mime::isPrintable($value)) {
			return $value;
		} else {
			/**
			 * Next strings fixes the problems
			 * According to RFC 1522 (http://www.faqs.org/rfcs/rfc1522.html)
			 */
			$quotedValue = '';
			$count = 1;
			for ($i = 0; strlen($value) > $i; $i++) {
				if ($value[$i] == '?' or $value[$i] == '_' or $value[$i] == ' ') {
					$quotedValue .= str_replace(array('?', ' ', '_'), array('=3F', '=20', '=5F'), $value[$i]);
				} else {
					$quotedValue .= $this->encodeQuotedPrintable($value[$i]);
				}
				if (strlen($quotedValue) > $count * Zend_Mime::LINELENGTH) {
					$count++;
					$quotedValue .= "?=\n =?" . $this->_charset . '?Q?';
				}
			}
			return '=?' . $this->_charset . '?Q?' . $quotedValue . '?=';
		}
	}
}